home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / news / inn1.000 / inn1.4sec-linux-src.tar / inn / frontends / getlist.c < prev    next >
C/C++ Source or Header  |  1993-03-18  |  4KB  |  187 lines

  1. /*  $Revision: 1.4 $
  2. **
  3. **  Get a file list from an NNTP server.
  4. */
  5. #include "configdata.h"
  6. #include <stdio.h>
  7. #include <errno.h>
  8. #include <sys/types.h>
  9. #include "clibrary.h"
  10. #include "libinn.h"
  11. #include "qio.h"
  12. #include "paths.h"
  13. #include "macros.h"
  14.  
  15.  
  16. /*
  17. **  Print usage message and exit.
  18. */
  19. STATIC NORETURN
  20. Usage()
  21. {
  22.     (void)fprintf(stderr, "Usage: getlist [-h host] [type [pat [groups]]\n");
  23.     exit(1);
  24. }
  25.  
  26.  
  27. int
  28. main(ac, av)
  29.     int        ac;
  30.     char    *av[];
  31. {
  32.     FILE    *active;
  33.     FILE    *FromServer;
  34.     FILE    *ToServer;
  35.     QIOSTATE    *qp;
  36.     char    *field4;
  37.     char    *types;
  38.     char    *host;
  39.     char    *line;
  40.     char    *list;
  41.     char    *p;
  42.     char    *pattern;
  43.     char    buff[512 + 1];
  44.     int        i;
  45.  
  46.     /* Set defaults. */
  47.     host = NULL;
  48.     pattern = NULL;
  49.     types = NULL;
  50.  
  51.     /* Parse JCL. */
  52.     while ((i = getopt(ac, av, "h:")) != EOF)
  53.     switch (i) {
  54.     default:
  55.         Usage();
  56.         /* NOTREACHED */
  57.     case 'h':
  58.         host = optarg;
  59.         break;
  60.     }
  61.     ac -= optind;
  62.     av += optind;
  63.  
  64.     /* Parse parameters. */
  65.     switch (ac) {
  66.     default:
  67.     Usage();
  68.     /* NOTREACHED */
  69.     case 0:
  70.     case 1:
  71.     break;
  72.     case 2:
  73.     pattern = av[1];
  74.     break;
  75.     case 3:
  76.     pattern = av[1];
  77.     types = av[2];
  78.     break;
  79.     }
  80.     if (av[0] == NULL)
  81.     list = "active";
  82.     else {
  83.     list = av[0];
  84.     if (EQ(list, "active") && (pattern != NULL || types != NULL))
  85.         Usage();
  86.     }
  87.  
  88.     /* Open a connection to the server. */
  89.     if (host == NULL
  90.      && (host = GetConfigValue(_CONF_SERVER)) == NULL) {
  91.     (void)fprintf(stderr, "Can't get server name, %s\n", strerror(errno));
  92.     exit(1);
  93.     }
  94.     buff[0] = '\0';
  95.     if (NNTPconnect(host, &FromServer, &ToServer, buff) < 0) {
  96.     (void)fprintf(stderr, "Can't connect to server, %s\n",
  97.         buff[0] ? buff : strerror(errno));
  98.     exit(1);
  99.     }
  100.  
  101.     /* Get the data from the server. */
  102.     active = CAlistopen(FromServer, ToServer, EQ(list, "active") ? NULL : list);
  103.     if (active == NULL) {
  104.     (void)fprintf(stderr, "Can't retrieve data, %s\n", strerror(errno));
  105.     (void)fclose(FromServer);
  106.     (void)fclose(ToServer);
  107.     exit(1);
  108.     }
  109.  
  110.     /* Set up to read it quickly. */
  111.     if ((qp = QIOfdopen((int)fileno(active), QIO_BUFFER)) == NULL) {
  112.     (void)fprintf(stderr, "Can't read temp file, %s\n", strerror(errno));
  113.     (void)fclose(FromServer);
  114.     (void)fclose(ToServer);
  115.     exit(1);
  116.     }
  117.  
  118.     /* Scan server's output, displaying appropriate lines. */
  119.     i = 1;
  120.     while ((line = QIOread(qp)) != NULL) {
  121.     i++;
  122.  
  123.     /* No pattern means print all. */
  124.     if (pattern == NULL) {
  125.         (void)printf("%s\n", line);
  126.         continue;
  127.     }
  128.  
  129.     /* Get the group name, see if it's one we want. */
  130.     if ((p = strchr(line, ' ')) == NULL) {
  131.         (void)fprintf(stderr, "Line %d is malformed\n", i);
  132.         continue;
  133.     }
  134.     *p = '\0';
  135.     if (!wildmat(line, pattern))
  136.         continue;
  137.     *p = ' ';
  138.  
  139.     /* If no group types, we want them all. */
  140.     if (types == NULL) {
  141.         (void)printf("%s\n", line);
  142.         continue;
  143.     }
  144.  
  145.     /* Find the fourth field. */
  146.     if ((p = strchr(p + 1, ' ')) == NULL) {
  147.         (void)fprintf(stderr, "Line %d (field 2) is malformed.\n", i);
  148.         continue;
  149.     }
  150.     if ((p = strchr(p + 1, ' ')) == NULL) {
  151.         (void)fprintf(stderr, "Line %d (field 3) is malformed.\n", i);
  152.         continue;
  153.     }
  154.     field4 = p + 1;
  155.     if ((p = strchr(field4, ' ')) != NULL) {
  156.         (void)fprintf(stderr, "Line %d has more than 4 fields\n", i);
  157.         continue;
  158.     }
  159.  
  160.     /* Is this the type of line we want? */
  161.     if (strchr(types, field4[0]) != NULL)
  162.         (void)printf("%s\n", line);
  163.     }
  164.  
  165.     /* Determine why we stopped */
  166.     if (QIOerror(qp)) {
  167.     (void)fprintf(stderr, "Can't read temp file at line %d, %s\n",
  168.         i, strerror(errno));
  169.     i = 1;
  170.     }
  171.     else if (QIOtoolong(qp)) {
  172.     (void)fprintf(stderr, "Line %d is too long\n", i);
  173.     i = i;
  174.     }
  175.     else
  176.     i = 0;
  177.  
  178.     /* All done. */
  179.     CAclose();
  180.     (void)fprintf(ToServer, "quit\r\n");
  181.     (void)fclose(ToServer);
  182.     (void)fgets(buff, sizeof buff, FromServer);
  183.     (void)fclose(FromServer);
  184.     exit(i);
  185.     /* NOTREACHED */
  186. }
  187.